rm(list = setdiff(ls(), lsf.str()))
#Load data 2017
personality8<- read.dta13("Study 2/Original Data/cp08a_1p_EN.dta") 
personality14<- read.dta13("Study 2/Original Data/cp14g_EN_1.0p.dta")
politics8<-read.dta13("Study 2/Original Data/cv08a_1.1p_EN.dta")
politics14<-read.dta13("Study 2/Original Data/cv14g_EN_1.0p.dta")

#merge data
data_1 <- merge(personality8, personality14,by="nomem_encr", all=T)
data_2 <- merge(politics8, politics14,by="nomem_encr", all=T)
data <- merge(data_2, data_1,by="nomem_encr")

data_sub <- subset(data,is.na(rowMeans(with(data,data.frame(cp08a026, cp08a021,cp14g026, cp14g021))))==F)                       

data_sub$y14_sympPVV<- (car::recode(data_sub$cv14g213, "999=NA")/10)
data_sub$y08_sympPVV<- (car::recode(data_sub$cv08a085, "999=NA")/10)


#mean(data_sub$y08_sympPVV, na.rm=T)
#sd(data_sub$y08_sympPVV, na.rm=T)

#mean(data_sub$y14_sympPVV, na.rm=T)
#sd(data_sub$y14_sympPVV, na.rm=T)

#subset data

#change in sympathy is causing personality to change
Agree <-'Agree08= ~ cp08a026 + cp08a021 + cp08a031 + cp08a031+ cp08a036 +cp08a041+ cp08a046 +cp08a051 +cp08a056 +cp08a061+ cp08a066
Agree14= ~ cp14g026 +cp14g021 +cp14g031+ cp14g036+ cp14g041+ cp14g046 +cp14g051 +cp14g056 +cp14g061 +cp14g066
y14_sympPVV ~ y08_sympPVV
Agree08 ~ y08_sympPVV
Agree08 ~ 0*y14_sympPVV
Agree14 ~ Agree08 + y14_sympPVV 
'

fit<-sem(Agree,ordered=c("cp08a026", "cp08a021", "cp08a031", "cp08a031", "cp08a036", "cp08a041", "cp08a046", "cp08a051", "cp08a056", "cp08a061", "cp08a066", "cp14g026", "cp14g021","cp14g031", "cp14g036", "cp14g041", "cp14g046", "cp14g051", "cp14g056", "cp14g061", "cp14g066"), data=data_sub)
summary(fit, fit.measures=TRUE, standardized=T)
